package com.lvy.hczn.front.business.controller;

import cn.hutool.core.util.StrUtil;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.system.domain.business.GoodsBoxDetail;
import com.lvy.hczn.front.business.dto.SortFrmLoseDto;
import com.lvy.hczn.front.business.service.GoodsBoxDetailService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.exception.UtilException;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wcs/box/detail/")
@Slf4j
public class GoodsBoxDetailController extends BaseController {

    @Autowired
    private GoodsBoxDetailService goodsBoxDetailService;


    /**
     * 根据货箱编号查询详情列表
     *
     * @param detail
     * @return
     */
    @ApiOperation(value = "根据货箱编号查询详情列表", notes = "根据货箱编号查询详情列表")
    @PostMapping("selectBoxDetailByBoxCode")
    public ResultUtil selectBoxDetailByBoxCode(@RequestBody GoodsBoxDetail detail) {
        try {
            List<GoodsBoxDetail> list = goodsBoxDetailService.selectBoxDetailByBoxCode(detail);
            return ResultUtil.ok(list);
        } catch (Exception e) {
            log.error("selectBoxDetailByBoxCode:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 根据货箱编号删除数据
     *
     * @param detail
     * @return
     */
    @ApiOperation(value = "根据货箱编号删除数据（如采购入库重量不符）", notes = "根据货箱编号删除数据")
    @PostMapping("delPhysicalByBoxCode")
    public ResultUtil delPhysicalByBoxCode(@RequestBody GoodsBoxDetail detail) {
        try {
            if (detail == null || StrUtil.isEmpty(detail.getBoxCode())) {
                return ResultUtil.failed("参数为空");
            }
            goodsBoxDetailService.delPhysicalByBoxCode(detail.getBoxCode());
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("delPhysicalByBoxCode:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 报损
     *
     * @param sortFrmLoseDto
     * @return
     */
    @ApiOperation(value = "报损", notes = "根据ID修改")
    @PostMapping("updateFrmLoseById")
    public ResultUtil updateFrmLoseById(@RequestBody SortFrmLoseDto sortFrmLoseDto) {
        try {
            GoodsBoxDetail goodsBoxDetail = goodsBoxDetailService.updateFrmLoseById(sortFrmLoseDto);
            return ResultUtil.ok(goodsBoxDetail);
        } catch (Exception e) {
            log.error("updateFrmLoseById:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }

    /**
     * 临期报损，离开缓存位后逻辑处理
     *
     * @param goodsBoxDetailInfo
     * @return
     */
    @ApiOperation(value = "临期报损，离开缓存位后逻辑处理", notes = "临期报损，离开缓存位后逻辑处理")
    @PostMapping("frmLossLeaveBuffer")
    public ResultUtil frmLossLeaveBuffer(@RequestBody GoodsBoxDetail goodsBoxDetailInfo) {
        try {
            goodsBoxDetailService.frmLossLeaveBuffer(goodsBoxDetailInfo);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("updateFrmLoseById:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                return ResultUtil.failed("系统异常");
            }
        }
    }


}
